package org.tourism.controller;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;
import org.tourism.common.CommonResponse;
import org.tourism.entity.EmergencyInfo;
import org.tourism.service.EmergencyInfoService;

import java.util.List;

/**
 * <p>
 *  前端控制器
 * </p>
 *
 * @author wuling
 * @since 2024-07-05
 */
@RestController
@RequestMapping("/EmergencyInfos")
public class EmergencyInfoController {
    @Autowired
    private EmergencyInfoService emergencyInfoService;
    //查询当前应急信息
    @GetMapping
    public CommonResponse<List<EmergencyInfo>> getAllEmergencyInfo() {
        List<EmergencyInfo> emergencyInfoList = emergencyInfoService.selectAllEmergencyInfo();
        //数据库查询成功但数据库中没有任何数据
        if (emergencyInfoList.isEmpty()) {
            String msg="当前没有应急信息";
            return CommonResponse.createForSuccessMessage(msg);
        }else if(!emergencyInfoList.isEmpty()){
            // 创建成功响应，包含数据
            String msg="数据库查询成功";
            return CommonResponse.createForSuccess(msg,emergencyInfoList);
        }else {
            return CommonResponse.createForError(500,"服务器内部错误");
        }
    }
    @PostMapping("/create")
    //创建一条应急信息
    public CommonResponse<EmergencyInfo> createEmergencyInfo(@RequestBody EmergencyInfo emergencyInfo) {
         if (emergencyInfoService.insertEmergencyInfo(emergencyInfo)==1){
             String msg="数据库插入成功，应急信息创建成功";
             return CommonResponse.createForSuccessMessage(msg);
         }else {
             String msg="应急信息创建失败";
             return CommonResponse.createForError(msg);
         }
    }
    @DeleteMapping("/delete/{content}")
    public CommonResponse<String> deleteEmergencyInfo(@PathVariable String content) {
            // 执行删除操作
            int deleted = emergencyInfoService.deleteEmergencyInfoByEmergencyContent(content);

            if (deleted==1) {
                return CommonResponse.createForSuccessMessage("成功删除应急信息");
            } else {
                return CommonResponse.createForError("删除应急信息失败");
            }
    }
    @PutMapping("/update")
    public CommonResponse<String> updateEmergencyInfo(@RequestBody EmergencyInfo emergencyInfo) {
        int updated = emergencyInfoService.updateEmergencyInfo(emergencyInfo);
        if (updated==1) {
            return CommonResponse.createForSuccessMessage("应急信息修改成功");
        }else {
            return CommonResponse.createForError("应急信息修改失败");
        }
    }
}

